<template>
	<view>
		<!-- 搜索 -->
		<view class="searbox">
			<view class="censear">
				<image :src="BASE_IMG_URL+'/index/search.png'" class="searimg"></image>
				<input type="text" placeholder="请输入关键词搜索" v-model="name" @input="queryName" class="lself" placeholder-style="color:#999">
			</view>
			<view class="querylist">
				<view class="querysec" @click="changeAll">
					{{isSelectAll ? '取消' : '全选'}}
				</view>
			</view>
		</view>
		
		<scroll-view class="scrollbox" scroll-y @scrolltolower="loadMore">
			<view class="tem_list">
				<view class="tem_li" v-for="(item,index) in list" :key="index" @click="selectItem(item, index)">
					<label class="radio">
						<radio value="r1" color="#008EFF" :checked="item.isSelect" class="selfradio" />
						<view class="tem_con">{{item.serial}}{{item.remark != '' ? '--'+item.remark:''}}</view>
					</label>
				</view>
				<view class="noData" v-if="list.length == 0">暂无更多</view>
			</view>
		</scroll-view>
		<view style="height:120rpx"></view>
		<view class="botwarp">
			<view class="botbtn removebtn" @click="quxiao">取消</view>
			<view class="botbtn" @click="queding">确定</view>
		</view>
	</view>
</template>

<script>
	import { netPrevSerial } from '@/api/api.js'
	import { BASE_IMG_URL } from '@/api/http.js'
	
	export default{
		data() {
			return{
				BASE_IMG_URL:BASE_IMG_URL,
				goods_id:0,
				page:1,
				name:'',
				totalPage:1,
				list:[],
				checkobj:{},
				isSelectAll:false,   //是否全选
			}
		},
		onLoad(options) {
			if(options.goods_id) {
				this.goods_id = options.goods_id
			}
		},
		onShow() {
			this.init()
		},
		
		methods:{
			init(){
				this.page = 1
				this.totalPage = 1
				this.list = []
				this.getList()
			},
			loadMore() {
				if(this.page >= this.totalPage){
					return
				}
				this.page ++
				this.getList()
			},
			getList() {
				let params = {
					name:this.name,
					goods_id:this.goods_id,
					page:this.page,
					limit:20
				}
				netPrevSerial(params).then(res=>{
					let data = res.data.data
					data.forEach(ele=>{
						ele.isSelect = false
					})
					this.list = this.list.concat(data)
					this.totalPage = res.data.last_page
				})
			},
			selectItem(item, index){
				this.checkobj = item
				let arr = this.list
				arr[index].isSelect = !arr[index].isSelect
				this.list = arr
			},
			queryName(e) {
				this.init()
			},
			queding() {
				let arr = []   //内存 数据
				let listarr = this.list
				listarr.forEach(ele=>{
					if(ele.isSelect){
						arr.push(ele)
					}
				})
				// uni.setStorageSync('serials',arr)
				this.$store.commit('SERIALS',arr)
				uni.navigateBack({
					delta:1
				})
			},
			//是否全选
			changeAll() {
				if(this.list.length == 0) {
					return
				}
				this.isSelectAll = !this.isSelectAll
				let arr = this.list
				if(this.isSelectAll){
					arr.forEach(ele=>{
						ele.isSelect = true
					})
				}else{
					arr.forEach(ele=>{
						ele.isSelect = false
					})
				}
				this.list = arr
			},
			quxiao() {
				uni.navigateBack({
					delta:1
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.searbox{
		padding:10rpx 0 20rpx 0;
		display: flex;
		justify-content: center;
		align-items: center;
		background:$uni-btn-bg-color;
		.censear{
			display: flex;
			justify-content: flex-start;
			align-items: center;
			padding:0 24rpx;
			border-radius: 34rpx;
			background:#FFFFFF;
			width:550rpx;
			height:68rpx;
			.searimg{
				width:48rpx;
				height:48rpx;
			}
			.lself{
				font-size:26rpx;
				color:#999;
			}
		}
		.querylist{
			height:100%;
			display: flex;
			justify-content: flex-end;
			align-items: center;
			.queryfir{
				padding:15rpx 0;
				width:120rpx;
				flex-shrink: 0;
			}
			.querysec{
				padding:15rpx;
				color:#fff;
			}
		}
		.toadd{
			width:100rpx;
			text-align: center;
			font-size:26rpx;
			color:#fff;
			margin-left:20rpx;
		}
	}
	
	.scrollbox{
		width:100%;
		height:calc(100vh - 222rpx);
	}
	.tem_list{
		padding:24rpx;
		.tem_li{
			padding:30rpx;
			background:#fff;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			border-bottom:1rpx solid #f5f5f5;
			.radio{
				display: flex;
				justify-content: flex-start;
				align-items: center;
			}
			.selfradio{
				transform: scale(0.7);
			}
			.customerimg{
				width:80rpx;
				height:80rpx;
				border-radius: 50%;
				margin:0 24rpx;
			}
			.tem_con{
				font-size:26rpx;
				color:#333;
			}
		}
	}
	.botwarp{
		position: fixed;
		left:0;
		bottom:0;
		width:750rpx;
		background:#fff;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding:0 24rpx;
		box-sizing: border-box;
		box-shadow: 1rpx 1rpx 5rpx 3rpx rgba(0,0,0,0.1);
		height:120rpx;
		.aleardy{
			font-size:24rpx;
			color:$uni-text-color;
			flex-shrink: 0;
		}
		.bot_scrollbox{
			width:400rpx;
			.botlist{
				display: flex;
				justify-content: flex-start;
				align-items: center;
				.botli{
					margin-right:24rpx;
					.botimg{
						width:60rpx;
						height:60rpx;
						border-radius: 50%;
					}
					.botname{
						font-size:24rpx;
						color:#333;
					}
				}
			}
		}
		.botbtn{
			width:160rpx;
			color:#fff;
			background:$uni-btn-bg-color;
			height:80rpx;
			text-align: center;
			line-height: 80rpx;
			font-size:24rpx;
			border-radius: 10rpx;
		}
		.removebtn{
			border:1rpx solid #999999;
			background:none;
			color:#999999;
		}
	}
</style>
